package com.woniuxy.special.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.special.entity.SysUserInfo;
import com.woniuxy.special.exception.ManagerException;
import com.woniuxy.special.mapper.SysUserInfoMapper;
import com.woniuxy.special.service.SysUserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.special.utils.JwtUtil;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 吴彦祖
 * @since 2025年04月07日
 */
@Service
public class SysUserInfoServiceImpl extends ServiceImpl<SysUserInfoMapper, SysUserInfo> implements SysUserInfoService {

    //用户登录
    @Override
    public SysUserInfo login(String loginName, String loginPwd, String type, HttpServletResponse response) {
        //查询账号
        SysUserInfo sysUserInfo = query().eq("LOGIN_NAME", loginName).one();
        if (sysUserInfo == null) throw new ManagerException(1234, "账号不存在");
        //比较密码
        if (!sysUserInfo.getLoginPwd().equals(loginPwd)) throw new ManagerException(1235, "密码错误");
        //设置响应头
        response.setHeader("token", JwtUtil.createToken(BeanUtil.beanToMap(sysUserInfo), 1800));
        //在跨域请求访问时 必须公开该请求头 前端才能获取到1
        response.setHeader("Access-Control-Expose-Headers", "token");
        return sysUserInfo;
    }
}
